package com.adl.hotelmanage.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adl.hotelmanage.util.DbUtil;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
		
		request.setCharacterEncoding("utf-8");
		  String user_userName=request.getParameter("user_userName");
		  String user_userPass=request.getParameter("user_userPass");
		  String user_userPass1=request.getParameter("user_userPass1");

		  String msg = "tt";
		  if(user_userPass.equals(user_userPass1)) {
		  Connection conn = DbUtil.getSQLConn();
			Statement sta = null;
			// 定义一个查询语句
			ResultSet rs = null;
			try {
				String sql = "select * from t_client where user_userName=" + "'" + user_userName + "'";
				sta = conn.createStatement();
				rs = sta.executeQuery(sql);
				System.out.println(rs);
		
				if(rs.next()==true) {
					
				
					
					msg = "ff";//重复注册
					System.out.println(msg);
				
				}
				//while*********
					else{
						
						 System.out.println(user_userName);
						 try { 
							 
								PreparedStatement ptmt=null;
							   String sql2 = "insert into t_client values('"+user_userName+"','"+user_userPass+"')";
							   ptmt = conn.prepareStatement(sql2);
							   ptmt.executeUpdate(sql2);
							   request.setAttribute("user_userName", user_userName);
							   request.setAttribute("user_userPass", user_userPass);
					        
			                   } catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
			                                      }
				 
					}
					
					//}//else********
				/*  response.getWriter().print(msg);*/
				
		                
		  }catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}        
		  }
		  
		  else {
			  msg = "hh";
		  }
		  System.out.println(msg);
				response.getWriter().print(msg);
				
	}

}
